//
//  TaskViewController.m
//  wsgz
//
//  Created by huck on 2017/5/4.
//  Copyright © 2017年 BoingPay. All rights reserved.
//

#import "TaskViewController.h"
#import "AppUtils.h"
#import "MJRefreshFooterView.h"
#import "MJRefreshHeaderView.h"
#import "HomeManager.h"
#import "WaterSearchTableViewCell.h"
#import "MyTabView.h"

static NSString* s_myTaskListCellIdentifier = @"WaterSearchListCellIdentifier";

@interface TaskViewController()<UIScrollViewDelegate,UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) MJRefreshFooterView *refreshFooterView;
@property (nonatomic, strong) MJRefreshHeaderView *refreshHeaderView;
@property (nonatomic, strong) MyTabView *tabView;
@property (nonatomic) BOOL isFirstLoad;

@property (nonatomic) TaskType currentTaskType; //当前搜索类型
@property (nonatomic) NSUInteger undonePage; //未办类型分页
@property (nonatomic) NSUInteger donePage; //已办类型分页
@property (nonatomic) BOOL undonePageMore; //未办类型更多
@property (nonatomic) BOOL donePageMore; //已办类型更多

@end


@implementation TaskViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initSubView];
}

- (void)dealloc
{
    [_refreshFooterView free];
    [_refreshHeaderView free];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self initData];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (void)initSubView
{
    _isFirstLoad = YES;
    _currentTaskType = TaskTypeUndone;
    [_searchTableView registerNib:[UINib nibWithNibName:@"WaterSearchTableViewCell" bundle:nil] forCellReuseIdentifier:s_myTaskListCellIdentifier];
    
    __weak TaskViewController *weakSelf = self;
    _refreshFooterView = [MJRefreshFooterView footer];
    _refreshFooterView.scrollView = _searchTableView;
    _refreshFooterView.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        [weakSelf pullUpRefresh];
    };
    
    _refreshHeaderView = [[MJRefreshHeaderView alloc] initWithScrollView:_searchTableView];
    _refreshHeaderView.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        [weakSelf pullDownRefresh];
    };

    [self initTabbarView];
//    [_refreshFooterView setStopRefresh:YES];
    _undonePage = 1;
    _donePage = 1;
    _undonePageMore = YES;
    _donePage = YES;
}

//tab切换
-(void)initTabbarView
{
    NSArray *names = @[@"待办任务",@"已办任务"];//@[@"待办任务",@"已办任务",@"我的关注"];
    NSInteger index = TaskTypeUndone;
    _tabView = [MyTabView tabViewWithFrameView:_tabbarFrame];
    [_tabView setTabTitles:names font:[UIFont systemFontOfSize:14] selColor:[UIColor colorWithString:COLOR_17A4FF] unselColor:[UIColor colorWithString:COLOR_989AA6]];
    [_tabView setSelectionChangeHandler:self action:@selector(onSelectTypeClick:)];
    [_tabView setIndicatorColor:[UIColor colorWithString:COLOR_17A4FF]];
    [_tabView selectIndex:index];
    [_tabView updateFrameWidth:SCREEN_WIDTH];
}

//tab切换
- (void)onSelectTypeClick:(NSNumber*)number
{
    if (nil == number) {
        return;
    }
    NSInteger index = [number integerValue];
    if(_currentTaskType != index){
        _currentTaskType = index;
        [_searchTableView setContentOffset:CGPointMake(0, 0) animated:NO];
        [_refreshFooterView endRefreshing];
        [_refreshHeaderView endRefreshing];
        switch (_currentTaskType) {
            case TaskTypeUndone:
                [_refreshFooterView setStopRefresh:!_undonePageMore];
                break;
            case TaskTypeDone:
                [_refreshFooterView setStopRefresh:!_donePageMore];
                break;
            default:
                break;
        }
        
        //切换加载和不加载区别
        //[_searchTableView reloadData];
        [self initData];
    }
}


//开始搜索
- (void)initData
{
    switch (_currentTaskType) {
        case TaskTypeUndone:
            _undonePage = 1;
            _undonePageMore = YES;
            break;
        case TaskTypeDone:
            _donePage = 1;
            _donePageMore = YES;
            break;
        default:
            break;
    }

    [self requestData:YES showLoading:YES];
    if(!_refreshHeaderView || ![_refreshHeaderView isRefreshing]){
        [_searchTableView setContentOffset:CGPointMake(0, 0)];
    }
}


- (void)requestData:(BOOL)reset showLoading:(BOOL)show
{
    if (show) {
        [Toast loading];
    }
    NSInteger page = _undonePage;
    if(_currentTaskType == TaskTypeDone){
        page = _donePage;
    }
    
    [[HomeManager sharedInstance] requestTaskList:_currentTaskType reset:reset page:page complete:^(BOOL success, BOOL hasMore) {
        _isFirstLoad = NO;
        if (_currentTaskType == TaskTypeDone ) {
            _donePageMore = hasMore;
            if(hasMore){
                _donePage++;
            }
        }else if (_currentTaskType == TaskTypeUndone) {
            _undonePageMore = hasMore;
            if(hasMore){
                _undonePage++;
            }
        }
        if (_refreshFooterView) {
            [_refreshFooterView endRefreshing];
            [_refreshFooterView setStopRefresh:!hasMore];
        }
        if (_refreshHeaderView) {
            [_refreshHeaderView endRefreshing];
        }
        if (show) {
            [Toast loaded];
        }
        if (!reset) {
            [_refreshFooterView endRefreshing];
        }
        
        [_searchTableView reloadData];
    }];
}

/**
 * 下拉刷新消息
 */
- (void)pullDownRefresh
{
    [self initData];
}

//上拉刷新
- (void)pullUpRefresh
{
    if ([AppUtils hasConnected:YES]) {
        [self requestData:NO showLoading:NO];
    }
    else {
        [_refreshFooterView endRefreshing];
    }
}

-(NSMutableArray *)getResultLsit{
    if (_currentTaskType == TaskTypeDone ) {
        return [[HomeManager sharedInstance] doneTaskList];
    }else if (_currentTaskType == TaskTypeUndone) {
        return [[HomeManager sharedInstance] undoneTaskList];

    }
    return [NSMutableArray array];
}

#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self hideInput];
    // 滚动到底部，自动加载
    if([AppUtils isScrollToBottom:scrollView] && ![_refreshFooterView isRefreshing] && !_refreshFooterView.stopRefresh) {
        [_refreshFooterView beginRefreshing];
    }
}

#pragma mark tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    _emptyView.hidden = [self getResultLsit].count>0 || _isFirstLoad;
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self getResultLsit].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WaterSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:s_myTaskListCellIdentifier];
    if (nil == cell) {
        return nil;
    }
    WaterInfoData *cellData = [self getResultLsit][indexPath.row];
    if(!cellData){
        return nil;
    }
    
    __weak typeof(self) weakSelf= self;

    cell.followBlock = ^(NSUInteger value,NSUInteger rowIndex) {
        [weakSelf follow:value index:rowIndex];
    };
    [cell setInfo:cellData index:indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}

- (void)follow:(NSInteger)value  index:(NSInteger) rowIndex
{
    NSString *s = [NSString stringWithFormat:@"%zi--%zi",value,rowIndex];
    [Toast makeText:s];
    
}


@end

